package com.olympia.myapp.activity;

import java.util.List;
import java.util.Random;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.olympia.myapp.R;
import com.olympia.myapp.base.BaseActivity;
import com.olympia.myapp.custom.Dialog_Confirm;
import com.olympia.myapp.custom.Dialog_Confirm.ProcessDialogConfirm;
import com.olympia.myapp.custom.Dialog_VCNV;
import com.olympia.myapp.custom.Dialog_VCNV.ProcessDialogVCNV;
import com.olympia.myapp.entity.QuestionVCNV;
import com.olympia.myapp.server.HttpConnection;
import com.olympia.myapp.util.AnimationControl;
import com.olympia.myapp.util.Constants;
import com.olympia.myapp.util.ControlListener;
import com.olympia.myapp.util.WaitingReminder;
import com.olympia.myapp.xml.QuestionXMLHandler;

public class VCNVActivity extends BaseActivity {
	TextView txtScore = null;
	TextView txtTime = null;
	Button btnOpen = null;
	AnimationControl animateControl = null;

	RelativeLayout layout1 = null;
	TextView btnCenter1 = null;
	RelativeLayout layout2 = null;
	TextView btnCenter2 = null;
	RelativeLayout layout3 = null;
	TextView btnCenter3 = null;
	RelativeLayout layout4 = null;
	TextView btnCenter4 = null;
	RelativeLayout layout5 = null;
	TextView btnCenter5 = null;
	RelativeLayout layout6 = null;
	TextView btnCenter6 = null;
	RelativeLayout layout7 = null;
	TextView btnCenter7 = null;
	RelativeLayout layout8 = null;
	TextView btnCenter8 = null;

	List<QuestionVCNV> arrQuestion = null;
	QuestionVCNV currQuestion = null;

	int idO = 0;
	int idOO = 0;
	int idOP = 0;
	int idOX = 0;

	int currLay = 0;

	boolean opened1 = false;
	boolean opened2 = false;
	boolean opened3 = false;
	boolean opened4 = false;
	boolean opened5 = false;
	boolean opened6 = false;
	boolean opened7 = false;
	boolean opened8 = false;

	int maxNum = 4;
	WaitingReminder waitingRemind = null;
	int timeWaiting = 36 * 1000;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vcnvactivity);
		intent = getIntent();

		animateControl = new AnimationControl();

		txtScore = (TextView) findViewById(R.id.vcnv_layout_right_score);
		txtScore.setText(String.valueOf(Constants.SCORE));
		txtTime = (TextView) findViewById(R.id.vcnv_layout_right_time);
		btnOpen = (Button) findViewById(R.id.btnOpen);
		showBtnOpen(false);
		btnOpen.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (maxNum > 0) {
					maxNum--;
					// đặt trạng thái đã mở dòng này
					RelativeLayout lay = null;
					if (currLay == 1) {
						lay = layout1;
						opened1 = true;
					} else if (currLay == 2) {
						lay = layout2;
						opened2 = true;
					} else if (currLay == 3) {
						lay = layout3;
						opened3 = true;
					} else if (currLay == 4) {
						lay = layout4;
						opened4 = true;
					} else if (currLay == 5) {
						lay = layout5;
						opened5 = true;
					} else if (currLay == 6) {
						lay = layout6;
						opened6 = true;
					} else if (currLay == 7) {
						lay = layout7;
						opened7 = true;
					} else {
						lay = layout8;
						opened8 = true;
					}
					// hiển thị dialog với câu hỏi trong tương ứng
					currQuestion = arrQuestion.get(currLay - 1);
					showDialogQuestion(lay, currQuestion.content,
							currQuestion.words, 15);
					showBtnOpen(false);
				}
			}
		});

		//
		idO = R.drawable.ic_o;
		idOO = R.drawable.ic_oo;
		idOP = R.drawable.ic_op;
		idOX = R.drawable.ic_ox;

		layout1 = (RelativeLayout) findViewById(R.id.layout_1);
		layout1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened1) {
					showBtnOpen(true);
					changeBackground(layout1, idOO, false);

					checkOpenedLayout(1);
					currLay = 1;
				}
			}
		});
		layout2 = (RelativeLayout) findViewById(R.id.layout_2);
		layout2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened2) {
					showBtnOpen(true);
					changeBackground(layout2, idOO, false);

					checkOpenedLayout(2);
					currLay = 2;
				}
			}
		});
		layout3 = (RelativeLayout) findViewById(R.id.layout_3);
		layout3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened3) {
					showBtnOpen(true);
					changeBackground(layout3, idOO, false);

					checkOpenedLayout(3);
					currLay = 3;
				}
			}
		});
		layout4 = (RelativeLayout) findViewById(R.id.layout_4);
		layout4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened4) {
					showBtnOpen(true);
					changeBackground(layout4, idOO, false);

					checkOpenedLayout(4);
					currLay = 4;
				}
			}
		});
		layout5 = (RelativeLayout) findViewById(R.id.layout_5);
		layout5.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened5) {
					showBtnOpen(true);
					changeBackground(layout5, idOO, false);

					checkOpenedLayout(5);
					currLay = 5;
				}
			}
		});
		layout6 = (RelativeLayout) findViewById(R.id.layout_6);
		layout6.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened6) {
					showBtnOpen(true);
					changeBackground(layout6, idOO, false);

					checkOpenedLayout(6);
					currLay = 6;
				}
			}
		});
		layout7 = (RelativeLayout) findViewById(R.id.layout_7);
		layout7.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened7) {
					showBtnOpen(true);
					changeBackground(layout7, idOO, false);

					checkOpenedLayout(7);
					currLay = 7;
				}
			}
		});
		layout8 = (RelativeLayout) findViewById(R.id.layout_8);
		layout8.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!opened8) {
					showBtnOpen(true);
					changeBackground(layout8, idOO, false);

					checkOpenedLayout(8);
					currLay = 8;
				}
			}
		});

		btnCenter1 = (TextView) findViewById(R.id.layout_1_btncenter);
		btnCenter2 = (TextView) findViewById(R.id.layout_2_btncenter);
		btnCenter3 = (TextView) findViewById(R.id.layout_3_btncenter);
		btnCenter4 = (TextView) findViewById(R.id.layout_4_btncenter);
		btnCenter5 = (TextView) findViewById(R.id.layout_5_btncenter);
		btnCenter6 = (TextView) findViewById(R.id.layout_6_btncenter);
		btnCenter7 = (TextView) findViewById(R.id.layout_7_btncenter);
		btnCenter8 = (TextView) findViewById(R.id.layout_8_btncenter);

		// đọc danh sách câu hỏi VCNV
		if (QuestionXMLHandler.qsVCNV != null)
			arrQuestion = QuestionXMLHandler.qsVCNV;
		// hiển thị ô chữ
		QuestionVCNV question = null;
		for (int i = 0; i < arrQuestion.size() - 1; i++) {
			question = arrQuestion.get(i);
			addButtonPosition(i, question.words.toCharArray(),
					question.words.length(),
					Integer.parseInt(question.keyindex));
		}
		// hiển thị dialog để phát âm thanh
		Constants.myService.playVCNV();

		showDialogWaiting();
		waitingRemind = new WaitingReminder(new ControlListener() {
			public void runTask() {
				Constants.myService.playVCNVBegin();
				// tắt dialog
				dgBegin.dismiss();
			}
		});
		waitingRemind.start(timeWaiting);

	}

	/**
	 * hiển thị dialog trước khi bắt đầu
	 */
	Dialog_Confirm dgBegin;

	private void showDialogWaiting() {
		dgBegin = new Dialog_Confirm(VCNVActivity.this,
				getString(R.string.note_title),
				"PHẦN THI VƯỢT CHƯỚNG NGẠI VẬT", false, false, null);
		dgBegin.setCancelable(false);
		dgBegin.show();
	}

	/**
	 * 
	 * @param idLayout
	 * @param len
	 * @param indexCenter
	 */
	private void addButtonPosition(int idLayout, char[] words, int len,
			int indexCenter) {
		if (indexCenter >= len) {
			indexCenter = len - 1;
		}

		RelativeLayout.LayoutParams lp = null;
		Bitmap bg = BitmapFactory.decodeResource(getResources(), idO);
		int w = bg.getWidth();
		int h = bg.getHeight();

		// ben trai
		int i = indexCenter - 1;
		while (i >= 0) {
			TextView txtAdd = new TextView(this);
			txtAdd.setClickable(false);
			int id = idLayout * 50 + i;
			txtAdd.setId(id);

			lp = new RelativeLayout.LayoutParams(w, h);
			txtAdd.setBackgroundResource(idO);
			txtAdd.setGravity(Gravity.CENTER);
			txtAdd.setText(String.valueOf(words[i]));
			txtAdd.setTextColor(getResources().getColor(R.color.transparent));

			TextView txtLen = null;
			if (i == 0) {
				txtLen = new TextView(this);
				txtLen.setId(5000);
				txtLen.setText(String.valueOf(len));
				txtLen.setGravity(Gravity.CENTER);
				txtLen.setTextColor(Color.RED);
			}
			switch (idLayout) {
			case 0:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_1_btncenter : id + 1);
				layout1.addView(txtAdd, lp);

				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.WRAP_CONTENT, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout1.addView(txtLen, para);
				}

				break;
			case 1:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_2_btncenter : id + 1);
				layout2.addView(txtAdd, lp);

				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout2.addView(txtLen, para);
				}
				break;
			case 2:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_3_btncenter : id + 1);
				layout3.addView(txtAdd, lp);
				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout3.addView(txtLen, para);
				}
				break;
			case 3:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_4_btncenter : id + 1);
				layout4.addView(txtAdd, lp);
				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout4.addView(txtLen, para);
				}
				break;
			case 4:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_5_btncenter : id + 1);
				layout5.addView(txtAdd, lp);
				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout5.addView(txtLen, para);
				}
				break;
			case 5:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_6_btncenter : id + 1);
				layout6.addView(txtAdd, lp);
				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout6.addView(txtLen, para);
				}
				break;
			case 6:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_7_btncenter : id + 1);
				layout7.addView(txtAdd, lp);
				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout7.addView(txtLen, para);
				}
				break;
			case 7:
				lp.addRule(RelativeLayout.LEFT_OF,
						i == indexCenter - 1 ? R.id.layout_8_btncenter : id + 1);
				layout8.addView(txtAdd, lp);
				if (txtLen != null) {
					RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(
							w, h);
					para.addRule(RelativeLayout.LEFT_OF, id);
					layout8.addView(txtLen, para);
				}
				break;
			default:
				break;
			}

			i--;
		}
		// bên phải
		i = indexCenter + 1;
		while (i < len) {
			int id = idLayout * 50 + i;
			TextView txtAdd = new TextView(this);
			txtAdd.setText(String.valueOf(words[i]));
			txtAdd.setId(id);
			txtAdd.setTextColor(getResources().getColor(R.color.transparent));
			txtAdd.setBackgroundResource(idO);
			txtAdd.setGravity(Gravity.CENTER);
			txtAdd.setClickable(false);

			lp = new RelativeLayout.LayoutParams(w, h);

			switch (idLayout) {
			case 0:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_1_btncenter : id - 1);
				layout1.addView(txtAdd, lp);
				break;
			case 1:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_2_btncenter : id - 1);
				layout2.addView(txtAdd, lp);
				break;
			case 2:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_3_btncenter : id - 1);
				layout3.addView(txtAdd, lp);
				break;
			case 3:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_4_btncenter : id - 1);
				layout4.addView(txtAdd, lp);
				break;
			case 4:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_5_btncenter : id - 1);
				layout5.addView(txtAdd, lp);
				break;
			case 5:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_6_btncenter : id - 1);
				layout6.addView(txtAdd, lp);
				break;
			case 6:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_7_btncenter : id - 1);
				layout7.addView(txtAdd, lp);
				break;
			case 7:
				lp.addRule(RelativeLayout.RIGHT_OF,
						i == indexCenter + 1 ? R.id.layout_8_btncenter : id - 1);
				layout8.addView(txtAdd, lp);
				break;
			default:
				break;
			}
			i++;
		}

		switch (idLayout) {
		case 0:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter1.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter1.setVisibility(View.INVISIBLE);
			break;
		case 1:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter2.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter2.setVisibility(View.INVISIBLE);
			break;
		case 2:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter3.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter3.setVisibility(View.INVISIBLE);
			break;
		case 3:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter4.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter4.setVisibility(View.INVISIBLE);
			break;
		case 4:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter5.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter5.setVisibility(View.INVISIBLE);
			break;
		case 5:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter6.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter6.setVisibility(View.INVISIBLE);
			break;
		case 6:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter7.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter7.setVisibility(View.INVISIBLE);
			break;
		case 7:
			if (indexCenter >= 0 || indexCenter < len)
				btnCenter8.setText("" + words[indexCenter]);
			if (indexCenter >= len || indexCenter < 0)
				btnCenter8.setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}

	}

	/**
	 * kiểm tra xem nếu chưa mở thì đổi lại trạng thái
	 * 
	 * @param curNew
	 */
	private void checkOpenedLayout(int curNew) {
		if (currLay != curNew) {
			switch (currLay) {
			case 1:
				if (!opened1)
					changeBackground(layout1, idO, false);
				break;
			case 2:
				if (!opened2)
					changeBackground(layout2, idO, false);
				break;
			case 3:
				if (!opened3)
					changeBackground(layout3, idO, false);
				break;
			case 4:
				if (!opened4)
					changeBackground(layout4, idO, false);
				break;
			case 5:
				if (!opened5)
					changeBackground(layout5, idO, false);
				break;
			case 6:
				if (!opened6)
					changeBackground(layout6, idO, false);
				break;
			case 7:
				if (!opened7)
					changeBackground(layout7, idO, false);
				break;
			case 8:
				if (!opened8)
					changeBackground(layout8, idO, false);
				break;
			default:
				break;
			}
		}
	}

	/**
	 * thay đôi background
	 */
	private void changeBackground(RelativeLayout layout, int idDrawable,
			boolean isColor) {
		if (layout != null) {
			int count = layout.getChildCount();
			while (count >= 0) {
				View view = layout.getChildAt(count);
				if (view instanceof TextView && view.getId() != 5000) {
					view.setBackgroundResource(idDrawable);
					if (isColor) {
						((TextView) view).setTextColor(getResources().getColor(
								R.color.yellow));
					}
					if (idDrawable == idOO)
						animateControl.RunAnimations(this, view, R.anim.alpha);
				}
				count--;
			}
		}
	}

	/**
	 * 
	 */
	private void showBtnOpen(boolean isShow) {
		if (btnOpen != null) {
			btnOpen.setVisibility(isShow ? View.VISIBLE : View.GONE);
		}
	}

	/**
	 * Hiển thị dialog câu hỏi
	 * 
	 * @param content
	 */
	Dialog_VCNV dg_vcnv = null;

	private void showDialogQuestion(final RelativeLayout lay,
			final String content, final String word, final int time) {
		if (!isFinishing()) {
			dg_vcnv = new Dialog_VCNV(this, content, time,
					new ProcessDialogVCNV() {
						public void click_Cancel() {
							if (lay != null) {
								if (maxNum > 0) {
									checkAnswer(lay, "", word);
									txtTime.setText(String.valueOf(time));
								} else {
									checkAnswer(lay, "", word);
									// hết quyền trả lời hàng ngang
									typeConfirm = 1;
									if (!isShowType1)
										showDialogConfirm(
												getString(R.string.note_title),
												getString(R.string.colum_note));
								}
							} else {
								// hien thi dialog qua man
								checkAnswer(lay, "", word);
							}
						}

						public void click_Run(String time) {
							if (dg_vcnv.isShowing()) {
								if (!isFinishing())
									Constants.myService.playClick();
								// cập nhật thời gian
								txtTime.setText(time);
							} else
								// cập nhật thời gian
								txtTime.setText(String.valueOf(time));
						}

						public void click_Stop(String ans) {
							if (lay != null) {
								if (maxNum > 0) {
									checkAnswer(lay, ans, word);
									txtTime.setText(String.valueOf(time));
								} else {
									checkAnswer(lay, ans, word);
									// hết quyền trả lời các từ hàng ngang và
									// cgo trả lời
									// hàng dọc
									typeConfirm = 1;
									if (!isShowType1)
										showDialogConfirm(
												getString(R.string.note_title),
												getString(R.string.colum_note));
								}
							} else {
								// hien thi dialog qua man
								checkAnswer(lay, ans, word);
							}
						}
					});

			dg_vcnv.setContent(content);
			dg_vcnv.setCancelable(false);

			dg_vcnv.getWindow().setGravity(Gravity.TOP | Gravity.LEFT);
			dg_vcnv.show();
			animateControl.RunGroupAnimations(VCNVActivity.this, dg_vcnv,
					R.anim.slide_right);
		}
	}

	/*
	 * hien thi dialog confirm
	 */
	Dialog_Confirm dgConfirm = null;
	int typeConfirm = 0;
	boolean isShowType1 = false;

	private void showDialogConfirm(String title, String content) {
		if (!isFinishing()) {
			if (typeConfirm == 1) {
				isShowType1 = true;
			}
			dgConfirm = new Dialog_Confirm(VCNVActivity.this, title, content,
					false, true, new ProcessDialogConfirm() {
						public void click_Ok() {
						}

						public void click_Cancel() {
							if (typeConfirm == 0)
								// accept score
								acceptScore();
							else {
								stopSelectRow();
							}
						}
					});

			dgConfirm.setContent(content);
			dgConfirm.setCancelable(false);
			dgConfirm.show();
			dgConfirm.setTextOnButton("", "Tiếp tục");
		}
	}

	private void acceptScore() {
		String urlScore = urlUtil.acceptScore(Constants.myAccount.mail,
				String.valueOf(Constants.SCORE));
		requestServer(urlScore);
	}

	// ====================== Gui yeu cau len server ========================
	public void requestServer(String url) {
		Handler handler = new Handler() {
			@Override
			public void handleMessage(Message message) {
				switch (message.what) {
				case HttpConnection.DID_START: {
					break;
				}
				case HttpConnection.DID_SUCCEED: {
					String result = message.obj.toString();
					if (result.contains("<result>OK</result>")) {
						// chuyen sang mà hinh khác
						intent = new Intent(VCNVActivity.this, TTActivity.class);
						startActivity(intent);
						overridePendingTransition(R.anim.fase_in,
								R.anim.fade_out);

						finish();
					} else
						msgUtil.showToast(VCNVActivity.this,
								"Không thể cập nhật điểm lên server");
					break;
				}
				case HttpConnection.DID_ERROR: {
					msgUtil.showToast(VCNVActivity.this,
							"Không thể kết nối đến máy chủ, Xin vui lòng kết nối lại");
					break;
				}
				}
			}
		};

		new HttpConnection(handler).get(url);
	}

	/**
	 * đã trả lời hàng này
	 * 
	 * @param indexLayout
	 * @param strAns
	 * @param word
	 * @return
	 */
	private boolean checkAnswer(RelativeLayout indexLayout, String strAns,
			String word) {

		showBtnOpen(false);
		if (arrQuestion != null) {
			if (word.toUpperCase().compareTo(strAns.toUpperCase()) == 0) {
				// cộng điểm
				if (indexLayout != null)
					Constants.SCORE += 15;
				else
					Constants.SCORE += maxNum * 20;
				txtScore.setText(String.valueOf(Constants.SCORE));
				// phat âm thanh trả lời đúng
				if (!isFinishing()) {
					int ran = (new Random()).nextInt(100) % 2;
					if (ran == 0)
						Constants.myService.playCorrect1();
					else
						Constants.myService.playCorrect2();
				}
				// .........
				if (indexLayout != null)
					changeBackground(indexLayout, idOP, true);
				else {
					if (!isSubmit) {
						isSubmit = true;
						typeConfirm = 0;
						String str = String.format(
								getResources().getString(
										R.string.start_content_score), "VCNV",
								Constants.SCORE);
						showDialogConfirm(getString(R.string.note_title), str);
					}
				}
				//
				return true;
			} else
			// phát âm thanh trả lời sai
			if (!isFinishing())
				Constants.myService.playUnCorrect1();
		}

		// .....

		if (indexLayout != null)
			changeBackground(indexLayout, idOX, false);
		else {
			if (!isSubmit) {
				isSubmit = true;
				typeConfirm = 0;
				String str = String.format(
						getResources().getString(R.string.start_content_score),
						"VCNV", Constants.SCORE);
				showDialogConfirm(getString(R.string.note_title), str);
			}
		}
		return false;
	}

	/**
	 * dừng qua trình chọn hàng dọc và hiển thị trả lời hàng dọc
	 */
	boolean isStop = false;
	boolean isSubmit = false;

	private void stopSelectRow() {
		if (!isStop) {
			isStop = true;
			currQuestion = arrQuestion.get(arrQuestion.size() - 1);
			showDialogQuestion(null, currQuestion.content, currQuestion.words,
					30);
		}
	}

	/**
	 * 
	 * @param view
	 */
	public void clickAnswerColum(View view) {
		typeConfirm = 1;
		if (!isShowType1)
			showDialogConfirm(getString(R.string.note_title),
					getString(R.string.colum_note));
	}

	@Override
	public void onBackPressed() {
		Constants.myService.clearAll();
		super.onBackPressed();
	}
}
